let app = getApp();

import {
  request,
  requestHasToken
} from '../../service/network.js'
let formatTime = require('../../utils/util')
import  { previewImg,previewImgList }  from '../../behavior/previewImg/previewImg.js'
var dateTimePicker = require('../../utils/dateTimePicker.js');
Page({
  behaviors: [previewImg,previewImgList], //  继承behavior.js里面的properties，data，methods
  data: {
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2060,
    isSubmit: false,
    warn: "",
    pic: '',
    images:[
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602400135247&di=25539946f38b531a05b68addff770738&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F140820%2F1-140R0102434-52.jpg',
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602400135247&di=32f85ea6379d5b6da876fcbdf009847c&imgtype=0&src=http%3A%2F%2Fimg.kutoo8.com%2Fupload%2Fimage%2F73370865%2F140-150313102339%2520%25281%2529_960x540.jpg'
    ],
    group: {
      title: '大家快来报名哦!',
      type: 'average',
      examine:'未审核',
      groupTime:'',
    },
    hasToken: false
  },
  formSubmit: function (e) {
    // console.log('form发生了submit事件，携带数据为：', e.detail.value, this.data.warn);
    let _this = this
    let {
      title,
      content,
      type,
      total,
      endTime,
      endPersonNum,
    } = e.detail.value
    if(endPersonNum=='undefined'||endPersonNum==undefined||endPersonNum==''){endPersonNum=0}
    if(endTime=='undefined'||endTime==undefined){endTime='1'}
    if (!title) {
      this.setData({
        warn: "请填写红色星号的必填项！",
        isSubmit: true
      })
    } else {
      console.log(endPersonNum,groupTime)
      let groupTime = this.data.dateTimeArray1[0][this.data.dateTime1[0]]+'-'
          +this.data.dateTimeArray1[1][this.data.dateTime1[1]]+'-'+this.data.dateTimeArray1[2][this.data.dateTime1[2]]
              +' '+this.data.dateTimeArray1[3][this.data.dateTime1[3]]+':'+this.data.dateTimeArray1[4][this.data.dateTime1[4]]
      this.setData({
        warn: "",
        isSubmit: false,
        'group.title': title,
        'group.content': content,
        'group.groupTime': groupTime,
        'group.type': type,
        'group.total': total,
        'group.endTime': endTime,
        'group.endPersonNum': endPersonNum,
      })
      if (this.data.group.id) {
        requestHasToken({
          url: 'api/go/dutch/update',
          data: this.data.group,
        }).then((res) => {
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '/pages/goDutch/goDutch?id=' + this.data.group.id
            })
          }
        })
      } else {
        requestHasToken({
          url: 'api/go/dutch/save',
          data: this.data.group,
        }).then((res) => {
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '/pages/goDutch/goDutch?id=' + res.data.id
            })
          }
        })
      }
    }
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  handleTotal(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      'group.total': value
    })
  },
  validateNumber(val) {
    return val.replace(/[^0-9\.-]/g, '')
  },
  validateBlueTotal(e) {
    let value = e.detail.value
    if (parseFloat(value) != NaN) {
      value = parseFloat(e.detail.value)
    } else {
      value = 0
    }
    this.setData({
      'group.total': value
    })
  },
  handlePic(res) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        this.setData({
          'pic': tempFilePaths[0],
        })
        wx.uploadFile({
          url: App.config.baseApi + 'api/xcxFile/upload',
          header: {
            Authorization: 'Bearer ' + wx.getStorageSync('token'),
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8' // 默认值
          },
          filePath: this.data.pic,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: (res2) => {
            let groupic = JSON.parse(res2.data).url
            this.setData({
              'group.pic': groupic,
            })
          }
        })
      }
    })
  },
  loginPage() {
    if (wx.getStorageSync('token') ) {
      return true
    } else {
      return false
    }
  },
  changeGroupDateColumn: function (e) {
    const groupTime = e.detail.value
    this.setData({
      'group.groupTime': groupTime
    })
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  changeDate(e){
    this.setData({'group.groupTime':e.detail.value});
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTime(e){
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e){
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  onLoad(options) {
    //https://www.cnblogs.com/dreamstartplace/p/10826134.html
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
    //判断是否有数据
    var flag = false;
    for (var x in options) {
      flag = true;
    }
    if(this.loginPage()) {
      this.setData({
        hasToken:true
      })
    }
    if (flag) {
      requestHasToken({
        url: 'api/go/dutch/get',
        data: {
          id: options.id
        }
      }).then((res) => {
        if (res.data.code == 0) {
          const datagroup = res.data.data
          console.log(datagroup.groupTime,datagroup.groupTime.substr(14,2));
          this.setData({
            'group.id': datagroup.id,
            'group.title': datagroup.title,
            'group.content': datagroup.content,
            'group.groupTime': datagroup.groupTime,
            'group.type': datagroup.type,
            'group.location': datagroup.location == 'null' ? '' : datagroup.location,
            'group.pic': datagroup.pic == 'null' ? '' : datagroup.pic,
            'group.total': datagroup.total,
            'group.openid': datagroup.openid,
            'group.examine': datagroup.examine,
            'dateTime1[0]':parseInt(datagroup.groupTime.substr(2,2)),
            'dateTime1[1]':parseInt(datagroup.groupTime.substr(5,2))-1,
            'dateTime1[2]':parseInt(datagroup.groupTime.substr(8,2))-1,
            'dateTime1[3]':parseInt(datagroup.groupTime.substr(11,2)),
            'dateTime1[4]':parseInt(datagroup.groupTime.substr(14,2)),
          })

          

          if (this.data.group.pic) {
            this.setData({
              pic: App.config.baseApi + 'api/nt/xcxFile/' + this.data.group.pic
            })
          }
        }
      })
    } else {
      // const datetime = formatTime.formatTime(new Date())
      // console.log(Date.parse(new Date()));
      
      // this.setData({
      //   'group.groupTime': datetime.substr(0, 10),
      // })
    }
  }
})